package org.lixue.game.server.handler.socket;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

@Slf4j
public class MessageEncoder extends MessageToByteEncoder<String> {
    private final Charset charset = Charset.forName("UTF-8");

    @Override
    protected void encode(ChannelHandlerContext ctx,
                          String msg, ByteBuf out) throws Exception {
        log.debug("encode:" + msg);
        byte[] bytes = msg.getBytes(charset);
        out.writeInt(bytes.length);
        out.writeBytes(bytes);
    }
}
